package com.nwpu.controller;

import com.nwpu.common.Result;
import com.nwpu.domain.User;
import com.nwpu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author DPH
 * @date 2023/5/29
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("{userId}")
    public Result<User> getUserById(@PathVariable String userId) {
        User user = userService.getUserById(userId);
        return Result.of(user);
    }

    @PutMapping
    public Result<Boolean> updateUser(@RequestBody User user) {
        Boolean res = userService.updateUser(user);
        return Result.of(res);
    }

    @PostMapping
    public Result<Boolean> addUser(@RequestBody User user) {
        Boolean res = userService.saveUser(user);
        return Result.of(res);
    }

    @DeleteMapping("{userId}")
    public Result<Boolean> deleteUser(@PathVariable String userId) {
        Boolean res = userService.deleteUserById(userId);
        return Result.of(res);
    }
}
